package home.game.client.display;

import home.game.client.controllers.Application;
import home.game.client.controllers.GameController;
import home.game.client.display.models.PlayerType;
import home.game.client.display.panels.GamePanel;
import home.game.client.display.panels.TopScorePanel;
import home.game.client.display.panels.UserPanel;

import javax.swing.*;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 */
public class MainFrame {
    private static MainFrame ourInstance;
    private JFrame frame;
    private GamePanel gamePanel;
    private GameController gameController;


    public static synchronized MainFrame getInstance() {
        if (ourInstance == null) {
            ourInstance = new MainFrame();
            ourInstance.init();
        }
        return ourInstance;
    }

    private MainFrame() {
    }

    public void init() {
        gamePanel = new GamePanel();
        UserPanel userPanel = new UserPanel();
        gameController = new GameController();
        userPanel.addObserver(gameController);
        gameController.initController(gamePanel);
        userPanel.showLogin();

    }

    public void initFrame() {
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("Stuff");
        JMenuItem newGame = new JMenuItem("New Game");
        newGame.addActionListener(new NewGameAction());
        JMenuItem menuItem = new JMenuItem("Top Scores");
        menuItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new TopScorePanel().showDialog();
            }
        });
        menu.add(newGame);
        menu.add(menuItem);
        menuBar.add(menu);
        frame = new JFrame();
        frame.add(gamePanel.getGamePanel());
        frame.setJMenuBar(menuBar);
        frame.setLocation((int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2), (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }

    public void dispose() {
        frame.dispose();
    }


    private class NewGameAction extends AbstractAction {

        @Override
        public void actionPerformed(ActionEvent e) {
            MainFrame.getInstance().dispose();
            gameController.startNewGame();

        }
    }

    public void showEndGame(PlayerType user) {
        JDialog dialog = new JDialog();
        dialog.add(new JLabel(String.format("Winner [%s]", user)));
        dialog.setModal(true);
        dialog.setSize(100, 100);
        dialog.setLocation((int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2), (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2));
        dialog.setVisible(true);
        MainFrame.getInstance().dispose();
        gameController.startNewGame();
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(new NimbusLookAndFeel());
        } catch (UnsupportedLookAndFeelException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        Application.getInstance().initApplication();
        MainFrame.getInstance();

    }
}
